import Vue from 'vue'
import Router from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import NotFound from '@/views/Error/404'
Vue.use(Router)
    // 获取原型对象上的push函数
const originalPush = Router.prototype.push
    // 修改原型对象中的push方法
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
const router = new Router({
    mode: 'history',
    routes: [{
            name: 'layout',
            path: '/layout',
            component: () =>
                import ('@/views/Layout'),
            children: [{
                    name: 'home',
                    path: '/layout/home',
                    component: () =>
                        import ('@/views/Home')
                },
                {
                    name: 'aboutus',
                    path: '/layout/aboutus',
                    component: () =>
                        import ('@/views/Aboutus')
                },
                {
                    name: 'joinus',
                    path: '/layout/joinus',
                    component: () =>
                        import ('@/views/Joinus')
                },
                {
                    name: 'contactus',
                    path: '/layout/contactus',
                    component: () =>
                        import ('@/views/Contactus')
                },
                {
                    name: 'products',
                    path: '/layout/products',
                    component: () =>
                        import ('@/views/Products')
                },
                {
                    name: 'recruitment',
                    path: '/layout/recruitment',
                    component: () =>
                        import ('@/views/Recruitment')
                },
                {
                    path: '/',
                    redirect: '/layout/home'
                }
            ]
        },
        {
            path: '/',
            redirect: '/layout'
        },
        {
            name: 'login',
            path: '/login',
            component: () =>
                import ('@/views/Login')
        },
        {
            name: 'register',
            path: '/register',
            component: () =>
                import ('@/views/Register')
        },
        {
            path: '*',
            name: 'notFound',
            component: NotFound
        }
    ]
})

router.beforeEach((to, from, next) => {
    NProgress.start()
    let mobile = null
    if (sessionStorage.getItem('userInfo') && sessionStorage.getItem('userInfo') !== null) {
        mobile = JSON.parse(sessionStorage.getItem('userInfo')).mobile
    }
    if (to.path === '/login' || to.path === '/register') {
        // 如果是访问登录界面，如果用户会话信息存在，代表已登录过，跳转到主页
        if (mobile) {
            next({ path: '/' })
        } else {
            next()
        }
    } else {
        next()
    }
})
router.afterEach((to, from) => {
    NProgress.done()
    if (document.getElementById('router-content') && document.getElementById('router-content').scrollTo) {
        const scrollTop = document.getElementById('router-content').scrollTop || document.getElementById('router-content').scrollTop
        if (scrollTop > 0) {
            if (document.getElementById('router-content') && document.getElementById('router-content').scrollTop) document.getElementById('router-content').scrollTop = 0
        }
    }

})
export default router